/**     
 * @Title: BenchimarkServlet.java   
 * @Package me.xxy.mq.demo.servlet   
 * @Description: TODO
 * @author weiwei 
 * @date 2018年5月30日 下午5:49:46   
 * @version V1.0     
 */
package me.xxy.mq.demo.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.xxy.mq.core.Message;
import me.xxy.mq.core.MessageTrunk;
import me.xxy.mq.demo.MessageType;
import me.xxy.mq.demo.SpringBeanAware;
import me.xxy.mq.demo.entity.DemoMessage;

/**
 * @ClassName: BenchimarkServlet
 * @Description: TODO
 * @author weiwei
 * @date 2018年5月30日 下午5:49:46
 * 
 */
public class BenchimarkServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	private static final int TOTAL_OPERATIONS = 100000;

	public BenchimarkServlet() {
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取mt实例
		final MessageTrunk mt = (MessageTrunk) SpringBeanAware.getBean("messageTrunk");
		final Message message = new Message(MessageType.DEMO_MESSAGE, new DemoMessage("hello mt"));
		ConsoleWebSocket.socket.sendMessage("开始插入消息到MT");
		// 消息入MT
		long startTime = System.currentTimeMillis();

		List<Thread> tds = new ArrayList<Thread>();

		// 模拟50个线程同时put消息，一共操作10W次
		final AtomicInteger ind = new AtomicInteger();
		for (int i = 0; i < 50; i++) {
			Thread hj = new Thread(new Runnable() {
				public void run() {
					while (ind.getAndIncrement() < TOTAL_OPERATIONS) {
						mt.put(message);
					}
				}
			});
			tds.add(hj);
			hj.start();
		}

		try {
			for (Thread t : tds)
				t.join();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		long elapsed = System.currentTimeMillis() - startTime;
		ConsoleWebSocket.socket.sendMessage(((1000 * TOTAL_OPERATIONS) / elapsed) + " ops");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
